package com.readpost.mapper;

import com.readpost.doman.Dto.DraftArticleDto;
import com.readpost.doman.Entity.Articles;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface ArticleMapper {

    //发布文章
    @Insert("INSERT INTO articles (title, content,excerpt ,cover_image_url, status,created_at) " +
            "VALUES (#{title}, #{content}, #{coverImageUrl}, #{status}, #{createdAt})")
    void addArticle(Articles articles);

    //保存草稿文章
    @Insert("INSERT INTO articles (user_id,title, content,excerpt,cover_image_url,category_id,status,created_at) " +
            "VALUES (#{userId},#{title}, #{content},#{excerpt}, #{coverImageUrl},#{categoryId}, #{status}, now())")
    void addCreateArticle(DraftArticleDto articles);

    //修改草稿文章
    @Insert("UPDATE articles SET title = #{title}, content = #{content},excerpt=#{excerpt}, cover_image_url = #{coverImageUrl}, category_id=#{categoryId},updated_at = now() WHERE user_id = #{userId} AND status = #{status}")
    void upCreateArticle(DraftArticleDto articles);

    //通过作者id查询草稿文章是否存在
    @Select("SELECT COUNT(*) FROM articles WHERE status = #{status} AND user_id = #{userId}")
    int hasDraftArticle(Articles.Status status, Integer userId);


    //根据作者id获取草稿文章
    @Select("SELECT * FROM articles WHERE status = #{status} AND user_id = #{userId}")
    Articles getContentDraftArticle(Articles.Status status, Integer userId);

    List<Articles> list(Integer categoryId, String state, Integer userId);
}
